
using Com.Ctrip.Framework.Apollo;
using Consul;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Primitives;
using Repository.communal;
using Scheduling.Repository.Interface;
using Scheduling.Repository.lmpl;
using Scheduling.Services.Calendars;
using Scheduling.Services.Schedules;
using Scheduling.Services.Shifts;
using Scheduling.Services.TeamMembers;
using Scheduling.Services.Teams;
using System.Reflection;


namespace Scheduling.Api.Write
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();

            builder.Services.AddDbContext<MyDbContext>(options =>
                 options.UseMySql(
                     builder.Configuration.GetValue<string>("wxy"),
                     new MySqlServerVersion(new Version(9, 4)) // 指定 MySQL 版本
                 )
             );



            static void RegisterConsul()
            {
                try
                {
                    var consulClient = new ConsulClient(p => {
                        p.Address = new Uri("http://115.190.167.248:8500");//主机ip:8500
                    });
                    // 2. 确保健康检查端点可访问
                    var httpCheck = new AgentServiceCheck()
                    {
                        DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),
                        Interval = TimeSpan.FromSeconds(10),
                        HTTP = "http://115.190.170.6:7581/WeatherForecast", // 确认此端口服务是否运行,WeatherForecast是天气预报控制器，如果没有这个控制器可以手动创建一个
                        Timeout = TimeSpan.FromSeconds(5)
                    };
                    // 3. 添加标签方便识别
                    var registration = new AgentServiceRegistration()
                    {
                        Checks = new[] { httpCheck },
                        ID = $"schedwrite-{Guid.NewGuid()}", // 更易读的ID
                        Name = "schedwrite",                // 业务api名称,必须小写
                        Address = "115.190.170.6",                   // 纯IP正确
                        Port = 7581,                               // 端口与健康检查一致
                        Tags = new[] { "dotnet-service" }           // 添加标签
                    };

                    // 4. 添加错误处理
                    var result = consulClient.Agent.ServiceRegister(registration).Result;
                    Console.WriteLine($"服务注册成功! Status: {result.StatusCode}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Consul注册失败: {ex.Message}");
                }
            }

            builder.Configuration.AddApollo(builder.Configuration.GetSection("Apollo")).AddDefault().AddNamespace("systemread");

            var app = builder.Build();
            RegisterConsul();
            if (app.Configuration is IConfigurationRoot configurationRoot)
            {
                ChangeToken.OnChange(
                    () => configurationRoot.GetReloadToken(),
                    () => Console.WriteLine($"配置已更新:{app.Configuration.GetValue<string>("wxy")}"));
            }
            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                
            }
            app.UseSwagger();
            app.UseSwaggerUI();
            app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
            app.UseAuthorization();


            app.MapControllers();

            app.Run();
        }
    }
}
